﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using com.pro2e.console.service.core;
using com.pro2e.web.util;
using System.Text;
using com.pro2e.web.config;
using sexhealth.console.service.post.vo;
using System.Collections.Generic;
using NHibernate.Criterion;
using sexhealth.console.service.activity.vo;

/// <summary>
/// Summary description for ActivityService
/// </summary>
namespace sexhealth.console.service.post
{
    public class ActivityService : ServiceImpl
    {
        public ActivityService()
        {

        }

        public void CreateAnswer(AnswerVO answerVO)
        {
            answerVO.CreatedDate = DateTime.Now;
            DaoInsert(answerVO);
        }

        /// <summary>
        /// 計算項目數量
        /// </summary>
        /// <param name="activityId">活動Id</param>
        /// <param name="countAll">是否計算全部類別</param>
        /// <param name="answerType">類型</param>
        /// <param name="optionKey">項目欄位名稱</param>
        /// <param name="optionValue">項目值</param>
        /// <returns></returns>
        public int CountOptionCheck(int activityId, bool countAll, AnswerType answerType, string optionKey, object optionValue, DateTime? startDate, DateTime? endDate)
        {
            DetachedCriteria dCriteria = DetachedCriteria.For(typeof(AnswerVO));

            //單一類別計算
            if (!countAll)
            {
                dCriteria.Add(Expression.Eq("AnswerType", (int)answerType));
            }

            if (startDate != null)
            {
                dCriteria.Add(Expression.Ge("CreatedDate", startDate));
            }
            if (endDate != null)
            {
                dCriteria.Add(Expression.Le("CreatedDate", endDate));
            }

            dCriteria.Add(Expression.Eq("ActivityId", activityId));
            dCriteria.Add(Expression.Eq(optionKey, optionValue));

            return CountDetachedCriteriaRow(dCriteria);
        }
    }
}